package com.qf.file.controller;

import com.qf.base.dto.file.MarkDTO;
import com.qf.base.dto.file.QrCodeDTO;
import com.qf.base.dto.file.ZoomDTO;
import com.qf.base.result.R;
import com.qf.base.util.Base64Util;
import com.qf.base.util.SnowFlakeUtil;
import com.qf.file.model.GifWork;
import com.qf.file.service.GifService;
import com.qf.file.util.AliyunOSSUtil;

import com.qf.file.util.ImageUtil;
import com.qf.file.util.QrCodeUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.web.bind.annotation.*;
import com.qf.base.dto.file.ImgDTO;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package com.qf.file.controller
 * @Description: 文件服务接口
 * @Date 2022/8/3 16:51
 */
@RestController
@RequestMapping("qf/file")
public class FileController {
    @Resource
    private SnowFlakeUtil snowFlakeUtil;

    @Resource
    private GifService gifService;

    public static final String FILE_PATH="D:\\TEMP";

    /**
     * 上传图片的接口
     * @param imgDTO
     * @return
     * @throws Exception
     */
    @PostMapping("img")
    public R img(@RequestBody ImgDTO imgDTO) throws Exception {
        String base64 = imgDTO.getBase64();
        byte[] content = Base64.decodeBase64(base64);
        String fileName = snowFlakeUtil.nextId() + ".jpg";
        String url = AliyunOSSUtil.uploadImage(fileName, content);
        return R.ok("上传成功").setResult("url",url);
    }

    @PostMapping("zoom")
    public R zoom(@RequestBody ZoomDTO zoomDTO) throws Exception {
        String base64 = zoomDTO.getBase64();
        Integer width = zoomDTO.getWidth() == null ? 100 : zoomDTO.getWidth();
        Integer height = zoomDTO.getHeight() == null ? 100 : zoomDTO.getHeight();

        //把base64的图片还原成BufferedImage
        BufferedImage image = Base64Util.base64ToImage(base64);

        //放缩
        BufferedImage zoom = ImageUtil.zoom(image, width, height);

        //把放缩之后的图片转化为base64
        base64 = Base64Util.image2Base64(zoom);

        byte[] content = Base64.decodeBase64(base64);
        String fileName = snowFlakeUtil.nextId() + ".jpg";
        String url = AliyunOSSUtil.uploadImage(fileName, content);
        return R.ok("上传成功").setResult("url",url);
    }

    /**
     * 图片右下角放水印
     * @param markDTO
     * @return
     */
    @PostMapping("waterMark")
    private R waterMark(@RequestBody MarkDTO markDTO) throws Exception {
        String base64 = markDTO.getBase64();

        //把图片由base64还原为BufferedImage
        BufferedImage bufferedImage = Base64Util.base64ToImage(base64);

        //印刷水印
        BufferedImage image = ImageUtil.waterMark(bufferedImage, markDTO.getWaterMark());

        //把结果图片变为base64
        base64= Base64Util.image2Base64(image);

        //拿到图片的字节数组
        byte[] content = Base64.decodeBase64(base64);
        String fileName = snowFlakeUtil.nextId() + ".jpg";
        String url = AliyunOSSUtil.uploadImage(fileName, content);
        return R.ok("设置水印成功").setResult("url",url);
    }

    /**
     * 水印全屏斜体
     * @param markDTO
     * @return
     */
    @PostMapping("italicMark")
    private R italicMark(@RequestBody MarkDTO markDTO) throws Exception {
        String base64 = markDTO.getBase64();

        //把图片由base64还原为BufferedImage
        BufferedImage bufferedImage = Base64Util.base64ToImage(base64);

        //d设置水印
        BufferedImage result = ImageUtil.italicMark(bufferedImage, markDTO.getWaterMark());

        //把结果图片变为base64
        base64 = Base64Util.image2Base64(result);

        //拿到结果图片的字节数组
        byte[] content = Base64.decodeBase64(base64);
        String fileName = snowFlakeUtil.nextId() + ".jpg";
        String url = AliyunOSSUtil.uploadImage(fileName, content);
        return R.ok("设置全屏水印成功").setResult("url",url);

    }

    /**
     * 生成二维码的接口
     */
    public R qrCode(@RequestBody QrCodeDTO qrCodeDTO){
        String content = qrCodeDTO.getContent();
        Integer width = qrCodeDTO.getWidth();
        Integer height = qrCodeDTO.getHeight();

        //#ffffff
        String codeColorStr = qrCodeDTO.getCodeColor().replace("#", "");
        Integer codeColor = Integer.parseInt(codeColorStr, 16) | (0xff << 24);

        String bgColorStr = qrCodeDTO.getBgColor().replace("#", "");
        int bgColor = Integer.parseInt(bgColorStr, 16) | (0xff << 24);

        BufferedImage bufferedImage = QrCodeUtil.qrEncode(content, width, height, codeColor, bgColor);
        String base64 = Base64Util.image2Base64(bufferedImage);

        return R.ok("二维码生成成功").setResult("base64",base64);
    }

    /**
     * 上传mp4文件生成gif
     */
    @RequestMapping("gif")
    public R gif(MultipartFile file){
        //生成文件名
        String fileName = snowFlakeUtil.nextId() + "";
        //把文件保存至服务器本地目录
        File targetFile = new File(FILE_PATH + File.separator + fileName + ".mp4");
        //写入到目标文件
        try {
            file.transferTo(targetFile);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("文件保存异常");
        }
        gifService.createGif(fileName);
        return R.ok("请求成功,请依据文件名查询处理结果").setResult("fileName",fileName);
    }

    @GetMapping("search")
    public R search(String fileName){
        GifWork gifWork = gifService.selectGifWork(fileName);

        if (gifWork.getState() == 0){
            return R.ok("后台正在处理中");
        } else if (gifWork.getState() == 1) {
            return R.ok("后台处理完成").setResult(gifWork);
        } else {
            return R.ok("后台处理失败");
        }
    }
}
